package com.example.browser;


import com.example.browser.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

public class BrowserProgress extends Activity {
	
	private WebView webview = null;
	private WebSettings ws = null;
	
	private LinearLayout progress_view;
	private int screenWidth;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browser_progress);
		
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);

		screenWidth = dm.widthPixels;
		
		progress_view = (LinearLayout) findViewById(R.id.progress_view);
		
		webview = (WebView) this.findViewById(R.id.WebView);
		webview.requestFocus();
		webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
		
		WebSettings webSettings = webview.getSettings();
		webSettings.setAllowFileAccess(true);
		webSettings.setSavePassword(true);
		webSettings.setSaveFormData(true);
		webSettings.setJavaScriptEnabled(true);
		webSettings.setBuiltInZoomControls(false);
		webSettings.setSupportZoom(false);
		
		webview.setWebViewClient(new MyWebViewClient());
		webview.setWebChromeClient(new MyWebChormClient());
		
		String loadUrl = "http://m.360buy.com";
		webview.loadUrl(loadUrl);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { 
			webview.goBack();
			return true; 
		} 
		return super.onKeyDown(keyCode, event);
	}
	
	private void showPageLoadProgress() {
		progress_view.setVisibility(View.VISIBLE);
	}
	
	private void hiddenPageLoadProgress() {
		progress_view.setVisibility(View.GONE);
	}
	
	class MyWebViewClient extends WebViewClient {

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
		
		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			Log.d("CRM", "onPageFinished, url="+url);
			hiddenPageLoadProgress();
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
			showPageLoadProgress();
		}
		
	}

	class MyWebChormClient extends WebChromeClient {

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			super.onProgressChanged(view, newProgress);
			int progressWidth = screenWidth*newProgress/100;
			progress_view.setLayoutParams(new LinearLayout.LayoutParams(progressWidth, LayoutParams.WRAP_CONTENT));
		}
		
	}

}
